################################# debian ####################################
# nginx:
alias nreload="nginx -s reload"
alias ntest="nginx -t"
alias nstop="nginx -s stop"
alias nquit="nginx -s quit"
alias nv="nginx -V"

# systemd相关：
alias sdreload='systemctl daemon-reload'
alias sta='systemctl status'
alias stt='systemctl start'
alias rstt='systemctl restart'
alias stp='systemctl stop'
alias sdenable='systemctl enable'
alias sddisable='systemctl disable'
alias sdcat='systemctl cat'
alias seh='systemctl list-units | grep'
alias sdshow='systemctl show'
alias jcf='journalctl -fu'
alias jcr='journalctl -ru'

function install_iperf3() {
    deb_url="https://iperf.fr/download/ubuntu/iperf3_3.9-1_amd64.deb"
    curl -sLO $deb_url && apt install iperf3_3.9-1_amd64.deb -y
}

# 端口相关：
# alias pl="netstat -tunlp"
# alias plist="netstat -tunlp | grep 0.0.0.0:"  # 查看所有绑定的端口
# alias psearch="netstat -tunlp | grep :"   # 搜索端口

pl() {
    if ! which netstat >/dev/null 2>&1; then
        apt update && apt install net-tools -y
    fi
    if [ $# != 1 ]; then
        netstat -tunlp | grep LISTEN
    else
        netstat -tunlp | grep $1
    fi
}

# # 新建redis实例，适用debian10:
# redis_new(){
#     if [ ! -n "$1" ] ;then
#         echo "请输入端口号：例如6379"
#         return
#     fi
#     cp -avx /etc/redis/redis.conf /etc/redis/redis-$1.conf
#     sed -i "s/port 6379/port $1/" /etc/redis/redis-$1.conf
#     # sed -i "s/pidfile \/var\/run\/redis\/redis.pid/pidfile \/var\/run\/redis-$1\/redis-server.pid/" /etc/redis/redis-$1.conf
#     sed -i "s/Type=forking/Type=notify/" /usr/lib/systemd/system/redis-server@.service
#     sed -i "s/supervised no/supervised systemd/" /etc/redis/redis-$1.conf
#     systemctl daemon-reload
#     systemctl start redis-server@$1
#     systemctl enable redis-server@$1
# }

# # 配置默认的redis，适用debian:
# redis_config_for_debian(){
#     sed -i "s/Type=forking/Type=notify/" /lib/systemd/system/redis-server.service
#     sed -i "s/supervised no/supervised systemd/" /etc/redis/redis.conf
#     systemctl daemon-reload
# }

# redis_enable_remote(){
#     sed -i "s/bind 127.0.0.1 ::1/# bind 127.0.0.1 ::1/" /etc/redis/redis.conf
#     sed -i "s/protected-mode yes/protected-mode no/" /etc/redis/redis.conf
# }

# redis_disable_remote(){
#     sed -i "s/# bind 127.0.0.1 ::1/bind 127.0.0.1 ::1/" /etc/redis/redis.conf
#     sed -i "s/protected-mode no/protected-mode yes/" /etc/redis/redis.conf
# }

# redis_enable_pwd(){
#     sed -i "s/# requirepass foobared/requirepass `$1`/" /etc/redis/redis.conf
# }

# redis_disable_pwd(){
#     sed -i "s/requirepass `$1`/# requirepass `$1`/" /etc/redis/redis.conf
# }

set.alias() {
    debian_set_alias
}

# # 适用debian10_mongodb4.4:
mongo_enable_remote() {
    sed -i "s/bindIp: 127.0.0.1/bindIp: 0.0.0.0/" /etc/mongod.conf
    systemctl restart mongod.service
}

# # 适用debian10_mongodb4.4:
# mongo_disable_remote(){
#     sed -i "s/bindIp: 0.0.0.0/bindIp: 127.0.0.1/" /etc/mongod.conf
#     systemctl restart mongod.service
# }

# # 适用debian10_mongodb4.4:
mongo_enable_auth() {
    sed -i "s/#security:/security:/" /etc/mongod.conf
    sed -i "/security:/a\    authorization: enabled" /etc/mongod.conf
    systemctl restart mongod.service
}

mongo_add_user() {
    user=$1
    pwd=$2
    db=$3
    echo 'db.createUser({user: "'"$user"'", pwd: "'"$pwd"'", roles: [{ role: "readWrite", db: "'"$db"'" }]})' | mongo $db
}

mongo_add_admin() {
    user=$1
    pwd=$2
    echo 'db.createUser({user: "'"$user"'", pwd: "'"$pwd"'", roles: [{ role: "userAdminAnyDatabase", db: "'"admin"'" }, "readWriteAnyDatabase"]})' | mongo admin
}

# 添加apt 源：
apt_add_source() {
    sed -i "/$('$1')/d" /etc/apt/sources.list
    echo $1 >>/etc/apt/sources.list
    apt update
}

# 查看apt 源：
apt_cat() {
    more /etc/apt/sources.list
}

# 安装apt-key：
apt_add_key() {
    apt -y install sudo apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    curl -fsSL $1 | sudo apt-key add -
}

# debian10安装docker-ce:
debian10_install_docker_ce() {
    if which docker >/dev/null 2>&1; then
        echo "--- docker is installed ---"
        docker --version
        docker-compose --version
        return 0
    fi

    source="deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/debian buster stable"
    sudo apt remove docker docker-engine docker.io containerd runc || true
    sudo apt -y install sudo apt-transport-https ca-certificates curl gnupg-agent software-properties-common
    sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo apt-key add -
    sudo sed -i "/$($source)/d" /etc/apt/sources.list
    sudo rm -rf /etc/apt/sources.list.d/docker.list 1>/dev/null 2>&1
    sudo echo $source | sudo tee -a /etc/apt/sources.list.d/docker.list 1>/dev/null 2>&1
    sudo echo $source >>/etc/apt/sources.list.d/docker
    sudo apt update && sudo apt install -y docker-ce docker-compose
    sudo docker --version
    sudo docker-compose --version
}

function mirror_tsinghua_11() {
    # debian11清华源
    cat >/etc/apt/sources.list <<EOF
deb https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free

deb https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free

deb https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
deb-src https://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
EOF
    apt update
}

function mirror_tsinghua_12() {
    # debian11清华源
    cat >/etc/apt/sources.list <<EOF
# 默认注释了源码镜像以提高 apt update 速度，如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
EOF
    apt update
}

function install_rsync() {
    apt update
    apt install rsync -y
}

# debian安装docker-ce,通用，不管debian哪个版本都可以用这个安装:
install_docker_ce() {
    if which docker >/dev/null 2>&1; then
        docker --version
    else
        echo "--- 准备安装docker ---"
        apt update &&
            apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates -y

        curl -sS https://download.docker.com/linux/debian/gpg | gpg --dearmor >/usr/share/keyrings/docker-ce.gpg

        echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian $(lsb_release -sc) stable" >/etc/apt/sources.list.d/docker.list

        # 安装docker:
        apt update &&
            apt install docker-ce docker-ce-cli containerd.io -y
        docker --version
    fi

    if which docker-compose >/dev/null 2>&1; then
        docker-compose --version
    else
        echo "--- 准备安装docker-compose!!! ---"
        # 安装docker-compose:
        rm -rf /usr/local/bin/docker-compose || true
        curl -L --retry 30 https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-x86_64 >/usr/local/bin/docker-compose
        chmod +x /usr/local/bin/docker-compose
        sudo docker-compose --version
    fi

}

function config_vnc_xfce() {
    apt install task-xfce-desktop dbus-x11 tightvncserver -y

    vncserver
    rm -rf ~/.vnc/xstartup.bak || true
    mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
    cat >~/.vnc/xstartup <<EOF
#!/bin/sh

# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

/usr/bin/startxfce4

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
x-window-manager &
EOF
    chmod +x ~/.vnc/xstartup
    vncserver -kill :1
    vncserver

    rm -rf /etc/systemd/system/vncserver.service || true
    cat >/etc/systemd/system/vncserver.service <<EOF
[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=root

ExecStart=/usr/bin/vncserver -depth 24 -geometry 1680x1050
ExecStop=/usr/bin/vncserver -kill :1

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl start vncserver
    systemctl enable vncserver

    # open vnc://192.168.245.143:5901
}

# 参考：
# https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-debian-11
# https://bytexd.com/how-to-install-configure-vnc-on-debian/
function config_vnc_lxqt() {
    apt install task-lxqt-desktop dbus-x11 tightvncserver -y

    vncserver
    rm -rf ~/.vnc/xstartup.bak || true
    mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
    cat >~/.vnc/xstartup <<EOF
#!/bin/sh

# Start up the standard system desktop
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS

/usr/bin/startlxqt

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
x-window-manager &
EOF
    chmod +x ~/.vnc/xstartup
    vncserver -kill :1
    vncserver

    rm -rf /etc/systemd/system/vncserver@.service
    cat >/etc/systemd/system/vncserver@.service <<EOF
[Unit]
Description=Start TightVNC server at startup
After=syslog.target network.target

[Service]
Type=forking
User=test
Group=test
WorkingDirectory=/home/test

PIDFile=/home/test/.vnc/%H:%i.pid
ExecStartPre=/usr/bin/vncserver -kill :%i > /dev/null 2>&1
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1280x800 :%i
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl start vncserver@1

    # open vnc://192.168.245.143:5901
}

# debian10安装mariadb:
install_mariadb() {
    apt update
    apt install mariadb-server -y
    mysql_secure_installation
}

# debian10安装redis5:
install_redis5() {
    apt update
    apt install redis-server -y
}

# debian10安装mongodb:
install_mongodb_4() {
    apt update
    apt install sudo dirmngr gnupg apt-transport-https software-properties-common ca-certificates curl -y
    curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
    add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/4.4 main'
    apt update
    apt install -y mongodb-org
    systemctl start mongod
}

# debian10安装nginx:
install_nginx() {
    apt update
    apt install nginx -y
}

# debian10安装go1.16开发环境:
install_go() {
    curl -OL https://studygolang.com/dl/golang/go1.16.2.linux-amd64.tar.gz &&
        tar xvf go1.16.2.linux-amd64.tar.gz &&
        rm -rf go1.16.2.linux-amd64.tar.gz &&
        chown -R root:root ./go &&
        mv go /usr/local &&
        echo "
    export GOPROXY=https://goproxy.io
    export GOPATH=$HOME/go
    export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
    " >>~/.zshrc &&
        source ~/.zshrc &&
        go get -v github.com/ramya-rao-a/go-outline &&
        go get -v golang.org/x/tools/gopls &&
        go get github.com/uudashr/gopkgs/v2/cmd/gopkgs &&
        go get github.com/go-delve/delve/cmd/dlv &&
        go get honnef.co/go/tools/cmd/staticcheck &&
        go version &&
        go env
}

alias ddf="docker system df"
alias vapt="vim /etc/apt/sources.list"
alias uuid="cat /proc/sys/kernel/random/uuid"

install_ssr() {
    port=$1
    pwd=$2

    mkdir -p /opt/ssr

    ufw allow $port

    curl "https://gitee.com/ElonWu/dotfiles/raw/master/ssr/server.config.json" >/opt/ssr/config.json

    sed -i "s/16823/$port/" /opt/ssr/config.json
    sed -i "s/pwd_xxx/$pwd/" /opt/ssr/config.json

    install_docker_ce
    docker run -d -p "$port":"$port" -p "$port":"$port"/udp --name ssr --restart=always -v /opt/ssr:/etc/shadowsocks-r teddysun/shadowsocks-r
    echo "ssr配置文件路径: /opt/ssr/config.json"
    cat /opt/ssr/config.json
}

ssr_show_config() {
    echo "ssr配置文件路径: /opt/ssr/config.json"
    cat /opt/ssr/config.json
}

uninstall_ssr() {
    docker stop ssr
    docker rm ssr
}

install_v2ray() {
    port=$1
    mkdir -p /opt/v2ray
    curl "https://gitee.com/ElonWu/dotfiles/raw/master/v2ray/server.config.json" >/opt/v2ray/config.json

    ufw allow $port
    sed -i "s/16823/$port/" /opt/v2ray/config.json

    install_docker_ce
    docker run -d -p "$port":"$port" -p "$port":"$port"/udp --name v2ray --restart=always -v /opt/v2ray:/etc/v2ray v2fly/v2fly-core
    echo "v2ray配置文件路径: /opt/v2ray/config.json"
    cat /opt/v2ray/config.json
}

install_pipenv() {
    apt install pipenv -y
}

install_node_npm_yarn() {
    apt install nodejs npm -y
    npm install -g yarn
}

clean_apt() {
    setopt localoptions rmstarsilent
    apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/
}

chrome_install_test() {
    google-chrome --headless --dumm-dom --user-data-dir --no-sandbox https://baidu.com
}

chrome_install() {
    # 安装google-chrome:
    wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee /etc/apt/sources.list.d/google-chrome.list
    apt update
    apt install -y google-chrome-stable

    # 安装中文字体, 解决chromedp截图乱码问题:
    setopt localoptions rmstarsilent
    apt-get update -y &&
        apt-get install -y fonts-noto &&
        apt-get install -y fonts-noto-cjk &&
        apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/
    echo ------------------------测试安装结果------------------------
    chrome_install_test
}

chrome_start_remote_debug_native() {
    google-chrome --remote-debugging-port=9222 --no-sandbox --headless
}

init_install() {
    apt update
    apt install -y psmisc # killall 命令
}
# 清空历史命令:
# hc() {
#     history -c
#     echo >$HOME/.bash_history
# }

test() {
    echo 'bbbbb'
}

# netdadta性能监控工具：
install_netdadta() {
    curl -s https://packagecloud.io/install/repositories/netdata/netdata/script.deb.sh | sudo bash
    apt update
    apt install netdata -y
}

nsq_service_file() {
    if [ $# != 1 ]; then
        echo "#### 使用方法: nsq_service_file service_name"
    fi
    name=$1
    cat >/opt/nsq/service/$name.service <<EOF
[Unit]
Description=$name service
After=network.target

[Service]
Type=simple
User=root
Restart=on-failure
RestartSec=5s
WorkingDirectory=/opt/nsq/
ExecStart=/opt/nsq/bin/$name
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target

EOF
    ln -s /opt/nsq/service/$name.service /etc/systemd/system/$name.service
}

install_nsq() {
    curl -O https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.2.1.linux-amd64.go1.16.6.tar.gz
    tar -zxvf nsq-1.2.1.linux-amd64.go1.16.6.tar.gz
    mv nsq-1.2.1.linux-amd64.go1.16.6 /opt/nsq/
    rm -rf nsq-1.2.1.linux-amd64.go1.16.6.tar.gz
    mkdir -p /opt/nsq/service

    nsq_service_file nsqd
    # nsq_service_file nsqlookupd
    # nsq_service_file nsqadmin
}

start_nsq() {
    nsqd --lookupd-tcp-address=127.0.0.1:4160
    nsqadmin --lookupd-http-address=127.0.0.1:4161
}

nsq_send() {
    topic=$1
    msg=$2
    curl -d $msg "http://127.0.0.1:4151/pub?topic=$1"
}

nsq_send1() {
    topic=$1
    msg=$2
    curl -d $msg "http://banli.site:4151/pub?topic=$1"
}

alias hc="hostnamectl"
alias hcset="hostnamectl set-hostname"

# 杀死占用端口的进程:
# kp() {
#     port=$1
#     kill -9 $(lsof -t -i tcp:$port)
# }

# 清理dns缓存:
dnsclean() {
    sudo service networking restart # 或者: sudo /etc/init.d/networking restart
}

vsshd() {
    vim /etc/ssh/sshd_config
}
